﻿using System.Web.Security;
using System;

namespace Ventigrate.Shared.ExternalMembership.Internal
{
    public static class Membership
    {
        #region Create

        public static MembershipUser CreateUser(string username, string password, string email)
        {
            MembershipUser result = null;

            using (var ctx = ExtranetContext.Default)
            {
                MembershipCreateStatus statusCode;
                result = ctx.MembershipProvider.CreateUser(username, password, email, null, null, true, null, out statusCode);
                if (result == null)
                {
                    throw new MembershipCreateUserException(statusCode);
                }
            }

            return result;
        }

        #endregion

        #region Update

        public static void UpdateUser(MembershipUser user)
        {
            using (var ctx = ExtranetContext.Default)
            {
                ctx.MembershipProvider.UpdateUser(user);
            }
        }

        #endregion

        #region Delete

        public static bool DeleteUser(string username)
        {
            bool result = false;

            using (var ctx = ExtranetContext.Default)
            {
                result = ctx.MembershipProvider.DeleteUser(username, true);
            }

            return result;
        }

        #endregion

        #region Get

        public static MembershipUser GetUser(string username)
        {
            MembershipUser result = null;

            using (var ctx = ExtranetContext.Default)
            {
                result = ctx.MembershipProvider.GetUser(username, false);
            }

            return result;
        }

        public static MembershipUserCollection GetAllUsers()
        {
            MembershipUserCollection result = null;

            using (var ctx = ExtranetContext.Default)
            {
                int num = 0;
                result = ctx.MembershipProvider.GetAllUsers(0, int.MaxValue, out num);
            }

            return result;
        }

        #endregion

        #region Properties

        public static bool EnablePasswordReset
        {
            get { return ExtranetContext.Default.MembershipProvider.EnablePasswordReset; }
        }

        public static bool EnablePasswordRetrieval
        {
            get { return ExtranetContext.Default.MembershipProvider.EnablePasswordRetrieval; }
        }

        public static bool RequiresQuestionAndAnswer
        {
            get { return ExtranetContext.Default.MembershipProvider.RequiresQuestionAndAnswer; }
        }

        #endregion
    }
}
